public class MaxSubarraySum {
 public static void main(String[] args) {
 int[] arr1 = {1, -2, 3, 5, -1};
 int[] arr2 = {1, -2, 3, -2, 5, 1};
 int[] arr3 = {1, -2, 3, -2, 5, 1};
 
 System.out.println("数组1的最大子数组和：" + maxSubArraySum(arr1));
 System.out.println("数组2的最大子数组和：" + maxSubArraySum(arr2));
 System.out.println("数组3的最大子数组和：" + maxSubArraySum(arr3));
 }
 
 // 使用 Kadane 算法求解
 public static int maxSubArraySum(int[] nums) {
 int maxCurrent = nums[0];
 int maxGlobal = nums[0];
 
 for (int i = 1; i < nums.length; i++) {
 maxCurrent = Math.max(nums[i], maxCurrent + nums[i]);
 if (maxCurrent > maxGlobal) {
 maxGlobal = maxCurrent;
 }
 }
 return maxGlobal;
 }
}